//
//  UIBarButton+extension.swift
//  FlyBlog
//
//  Created by FEI on 16/4/13.
//  Copyright © 2016年 FEI. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    /*便利构造函数:方便创造对象
    1,在分类里面只能使用便利构造函数
    2,便利构造函数最终需要调用一个指定构造函数
    3,调用遍历构造函数,保证调用这个遍历构造函数会调用的指定构造函数
    4,构造函数,不会省略第一个外部参数名
*/

    convenience init(imageName: String,target:AnyObject? , action: Selector) {
        let button = UIButton()

        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        self.init(customView:button)
    }

    class func creatBarButtonItem(imageName:String) -> UIBarButtonItem {
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        return UIBarButtonItem(customView: button)
    }

}
